package EP1.algoritmos;

import java.util.ArrayList;

public class Primes {

    private ArrayList<Integer> primeList;
    private ArrayList<Twin> twinList;
    private int threads;

    public Primes(ArrayList<Integer> primeList, int threads) {
	this.primeList = primeList;
	this.threads = threads;
    }
    
    public Primes(ArrayList<Integer> primeList, ArrayList<Twin> twinList, int threads) {
	this.primeList = primeList;
	this.twinList = twinList;
	this.threads = threads;
    }

    public boolean isPrime(int num) {
	int s = num / 2;
	int size = primeList.size();

	for (int i = 0; i < size; i++) {
	    int a = primeList.get(i);
	    if (a > s)
		break;
	    if (num % a == 0) {
		return false;
	    }
	}

	return true;

    }

    public void makePrimes(int n) {
	if (isPrime(2))
	    primeList.add(2);
	for (int i = 3; i <= n; i += 2) {
	    if (isPrime(i))
		primeList.add(i);
	}
    }

    public void makePrimesTo(int n) {
	if (isPrime(2))
	    primeList.add(2);
	for (int i = 3; primeList.size() < n; i += 2) {
	    if (isPrime(i))
		primeList.add(i);
	}
    }
    
    public void makePrimesToTwin(int n) {
	int n_twin = 0;
	if (isPrime(2)){
	    primeList.add(2);
	}
	for (int i = 3; n_twin < n; i += 2) {	    
	    if (isPrime(i)) {		
		primeList.add(i);
		int size = primeList.size();
		if (i - primeList.get(size - 2) == 2) {
		    if(twinList != null){
			twinList.add(new Twin(i, primeList.get(size - 2)));
		    }
		    n_twin++;
		}
	    }	    	
	}
    }

    public ArrayList<Integer> getPrimeList() {
	return primeList;
    }
}
